package main

import (
	"fmt"
	"gPublic/Viper"
	"gPublic/handle"
	"gPublic/routers"

	"net"

	// _ "gPublic/grpc/pubData/go" //引用protoc生成的go package
	pb2 "gPublic/grpc/yz-pub-data/openData"
	pb1 "gPublic/grpc/yz-pub-data/pubData"

	// handle "gPublic/handle"
	"gPublic/utils"

	"github.com/gin-gonic/gin"
	"github.com/soheilhy/cmux"
	"github.com/spf13/viper"
	"google.golang.org/grpc"
)

func InitConfig() {
	viper.SetConfigFile("config-develop.yml")
	err := viper.ReadInConfig()
	if err != nil {
		fmt.Println(err)
	}
}

func main() {
	router := gin.Default()
	routers.AppRoutes(router)

	InitConfig()
	Viper.ViperTest()

	// 初始化redis
	utils.InitRedis()

	// 启动多个grpc服务
	l, _ := net.Listen("tcp", ":5905")

	m := cmux.New(l)

	// grpc
	grpcL := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
	server := grpc.NewServer()
	pb2.RegisterOpenDataServer(server, &handle.ServerPcOpenData{})
	pb1.RegisterPbuDataServer(server, &handle.ServerPubData{})
	go server.Serve(grpcL)

	// http
	go func() {
		httpL := m.Match(cmux.HTTP1Fast())
		router.RunListener(httpL)
	}()

	// Start serving!
	m.Serve()

	// router.SetTrustedProxies([]string{"47.117.156.51"})
	// router.Run(":5905")
}
